﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities.Menu;
using Senparc.Weixin.MP.AdvancedAPIs;

namespace WXApi.Controllers
{
    public class MenuController : Controller
    {

        public static readonly string EncodingAESKey = Config.SenparcWeixinSetting.EncodingAESKey;//与微信公众账号后台的EncodingAESKey设置保持一致，区分大小写。
        public static readonly string AppId = Config.SenparcWeixinSetting.WeixinAppId;//与微信公众账号后台的AppId设置保持一致，区分大小写。
        public static readonly string appSecret = Config.SenparcWeixinSetting.WeixinAppSecret;//与微信公众账号后台的AppSecret设置保持一致，区分大小写。


        [HttpGet]
        [ActionName("Index")]
        public ActionResult Get(int cmd)
        {
            switch (cmd)
            {
                case 1:
                    SetMenu();
                    break;
                case 2 :
                   
                    break;
                default: 
                    break;
            }
            return Content(""); //返回随机字符串则表示验证通过 
        }


        private void SetMenu()
        {

            if (!AccessTokenContainer.CheckRegistered(AppId))//检查是否已经注册
            {
                AccessTokenContainer.Register(AppId, appSecret);//如果没有注册则进行注册
            }
            string access_token = AccessTokenContainer.GetAccessTokenResult(AppId).access_token;

            ButtonGroup bg = new ButtonGroup();

            String Url = "http://......(填写你的链接).....";
            String text = "<a href=" + Url + "?openid=\" + openid + \"\">点此链接注册</a>";

            //单击
            bg.button.Add(new SingleClickButton()
            {
                name = "获取网站入口",
                key = "OneClick",
                type = "click",//默认已经设为此类型，这里只作为演示
            });
            var result = CommonApi.CreateMenu(access_token, bg);
        }


    }
}